package com.yellowpage.bigdata.action;
  
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.bigdata.data.O2oLogSumDay;
import com.yellowpage.bigdata.service.O2oLogSumDayService;

import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;


public class O2oLogSumDayChannelAction extends BaseAction {

 
 
	
	private O2oLogSumDay o2oLogSumDay;
	private O2oLogSumDayService o2oLogSumDayService;
	
	private String selectedId;
	private String query_logDate;
	private String query_o2o_id;
	private String query_o2o_channel;
	private String query_logDate_end;
	private List<Map> resultList = new ArrayList<Map>();
	private Map O2oIdMap = new HashMap();
	private Map O2oChannelMap = new HashMap();
		
	public void prepare() {
		super.prepare();
	    //加载O2O_ID O2O_CHANNEL_ID
		O2oIdMap = o2oLogSumDayService.getO2oIdMap();
		O2oChannelMap =  o2oLogSumDayService.getO2oChannelMap();
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.o2oLogSumDay = o2oLogSumDayService.getO2oLogSumDay(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map<String,String> queryMap = new HashMap<String,String>();	
	    queryMap.put("o2oId", query_o2o_id);
	    queryMap.put("channelId", query_o2o_channel);
	    queryMap.put("logDate", query_logDate);
		queryMap.put("query_logDate_end", query_logDate_end);
	    	
		resultList = o2oLogSumDayService.queryO2oLogSumDayChannelByPage(queryMap, pageRender);
		
		setPageRenderToRequest();
		
		return "query";
	}


     //导出模块的编写
	//excel导出模块
	public void exportExcel()
	{
		initPageRender();
		Map<String,String> queryMap = new HashMap<String,String>();
		queryMap.put("o2oId", query_o2o_id);
		queryMap.put("channelId", query_o2o_channel);
		queryMap.put("logDate", query_logDate);
		queryMap.put("query_logDate_end", query_logDate_end);

		resultList = o2oLogSumDayService.queryO2oLogSumDayChannelByPage(queryMap, pageRender);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/x-download");
		OutputStream outp = null;
		response.addHeader("Content-Disposition", "attachment;filename="
				+ "ImeiDayRegion.xls");
		try {
			outp = response.getOutputStream();
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("O2OLogSunDayChannel");
			sheet.autoSizeColumn(1);
			HSSFCellStyle cellStyle = workbook.createCellStyle(); //单元格格式
			cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框
			cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中

			HSSFRow row1 = sheet.createRow(0);// 二级标题栏
			//日期	O2O_ID	O2O名称（备注）	渠道编号	渠道名称	UV（昨日）imei	UV（今日） imei	PV（昨日）请求数	PV（今日） 请求数	累计Uv	累计Pv
			String[] title = {"日期","O2O_ID","O2O名称（备注） ","渠道编号 ","渠道名称","UV（昨日）imei","UV（今日）imei",
					"PV（昨日）请求数","PV（今日） 请求数","累计Uv","累计Pv"};
			for(int i = 0 ,len = title.length;i<len;i++){
				HSSFCell cell = row1.createCell(i);
				cell.setCellValue(title[i]);
			}
			for (int i = 0, len = resultList.size(); i < len; i++) {
				HSSFRow data_row = sheet.createRow(i + 1);
					Map result = resultList.get(i);
				data_row.createCell(0).setCellValue(
						result.get("DAY_ID") != null ? result.get(
								"DAY_ID").toString() : "");
				data_row.createCell(1).setCellValue(
						result.get("O2O_ID") != null ? result.get(
								"O2O_ID").toString() : "");
				data_row.createCell(2).setCellValue(
						result.get("O2O_NAME") != null ? result.get(
								"O2O_NAME").toString() : "");
				data_row.createCell(3).setCellValue(
						result.get("CHANNEL_NO") != null ? result.get(
								"CHANNEL_NO").toString() : "");
				data_row.createCell(4).setCellValue(
						result.get("CHANNEL_NAME") != null ? result.get("CHANNEL_NAME")
								.toString() : "");

				data_row.createCell(5).setCellValue(
						result.get("YES_UV_VAL") != null ? result.get("YES_UV_VAL")
								.toString() : "");
				data_row.createCell(6).setCellValue(
						result.get("UV_VAL") != null ? result.get("UV_VAL")
								.toString() : "");
				data_row.createCell(7).setCellValue(
						result.get("YES_PV_VAL") != null ? result.get(
								"YES_PV_VAL").toString() : "");

				data_row.createCell(8).setCellValue(
						result.get("PV_VAL") != null ? result.get(
								"PV_VAL").toString() : "");
				data_row.createCell(9).setCellValue(
						result.get("SUM_UV_VAL") != null ? result.get(
								"SUM_UV_VAL").toString() : "");
				data_row.createCell(10).setCellValue(
						result.get("SUM_PV_VAL") != null ? result.get(
								"SUM_PV_VAL").toString() : "");


			}
			workbook.write(outp);
			response.flushBuffer();
			outp.flush();
			response.reset();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.addActionError("** 生成文件错误 !");
		}finally {
			if (outp != null) {
				try {
					outp.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}








	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setO2oLogSumDay(O2oLogSumDay o2oLogSumDay) {
		this.o2oLogSumDay = o2oLogSumDay;
	}
	
	public O2oLogSumDay getO2oLogSumDay() {
		return o2oLogSumDay;
	}
	 
	 
	 public void setO2oLogSumDayService(O2oLogSumDayService o2oLogSumDayService) {
		this.o2oLogSumDayService = o2oLogSumDayService;
	}
	
	 
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	 

	 
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	 
    
    
      
	public String getQuery_logDate() {
		return query_logDate;
	}

	public void setQuery_logDate(String query_logDate) {
		this.query_logDate = query_logDate;
	}

	public String getQuery_o2o_id() {
		return query_o2o_id;
	}

	public void setQuery_o2o_id(String query_o2o_id) {
		this.query_o2o_id = query_o2o_id;
	}

	public String getQuery_o2o_channel() {
		return query_o2o_channel;
	}

	public void setQuery_o2o_channel(String query_o2o_channel) {
		this.query_o2o_channel = query_o2o_channel;
	}

	public String getQuery_logDate_end() {
		return query_logDate_end;
	}

	public void setQuery_logDate_end(String query_logDate_end) {
		this.query_logDate_end = query_logDate_end;
	}

	public Map getO2oIdMap() {
		return O2oIdMap;
	}

	public void setO2oIdMap(Map o2oIdMap) {
		O2oIdMap = o2oIdMap;
	}

	public O2oLogSumDayService getO2oLogSumDayService() {
		return o2oLogSumDayService;
	}

	public Map getO2oChannelMap() {
		return O2oChannelMap;
	}

	public void setO2oChannelMap(Map o2oChannelMap) {
		O2oChannelMap = o2oChannelMap;
	}    

    
	
}
